﻿using SimpleInjector;
using Sustainalytics.Governance.Computing;
using Sustainalytics.Synchronization.Interface;

namespace Sustainalytics.Governance.Synchronization
{
    public class GovernanceComputationSyncStep : ISyncStep
    {
        public SyncStepOrder ExecutionOrder => SyncStepOrder.SyncGovernanceComputation;

        protected Container SimpleInjectorContainer;

        public SyncContext Start(SyncContext syncContext)
        {
            // check for task cancellation
            syncContext.Token.ThrowIfCancellationRequested();

            SimpleInjectorContainer = SimpleInjectorConfig.Register(new SyncContextLogger(syncContext), syncContext.GAConnectionStrings);

            SimpleInjectorContainer = Computing.SimpleInjectorConfig.Register(new SyncContextLogger(syncContext), syncContext.GAConnectionStrings);
            var computationEngine = SimpleInjectorContainer.GetInstance<GovernanceComputationEngine>();

            syncContext.Log("Governance Computation started");

            computationEngine.Compute();
            var items = computationEngine.Log;
            foreach (var item in items)
            {
                syncContext.Log(item);
            }

            syncContext.Log("Finalized governance computation");

            return syncContext;
        }

    }
}
